package com.neusoft.boot32.dibuy.repository.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neusoft.boot32.dibuy.entity.Account;
import com.neusoft.boot32.dibuy.entity.Author;
import com.neusoft.boot32.dibuy.mapper.AccountMapper;
import com.neusoft.boot32.dibuy.mapper.AuthorMapper;
import com.neusoft.boot32.dibuy.repository.IAccountRepsitory;
import com.neusoft.boot32.dibuy.repository.IAuthorRepsitory;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @program: springboot32
 * @description:
 * @author: Jingdi.Duan ,duanjingdi@msn.com
 * @create: 2024-07-11 08:11
 **/
@Component
public class AccountRepsitoryImpl extends ServiceImpl<AccountMapper, Account> implements IAccountRepsitory {

    @Override
    public List<Account> selectAll() {
        return baseMapper.selectList(null);
    }

    @Override
    public int addAccount(Account account) {
        int  i = baseMapper.insert(account);
        return i==1 ?  account.getAutoId().intValue()  : -1;
    }

    @Override
    public List<Account> selectByName(String name) {

        LambdaQueryWrapper<Account> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Account::getUsername, "admin")
                .allEq(Map.of( Account::getTel, "13674136186"   , Account::getPassword  , "666")  );

        return   baseMapper.selectList(lambdaQueryWrapper )  ;
    }

    @Override
    public int updatePassword(Integer autoid, String password) {

        LambdaUpdateWrapper<Account> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
        lambdaUpdateWrapper.eq(Account::getAutoId, autoid)
                .set(Account::getPassword, password);

        if( baseMapper.update(null, lambdaUpdateWrapper) == 1 )
            return 1;

        return 0;
    }

    @Override
    public int modAccount(Account account) {
//        默认mybatisplus  忽略空属性
//        if( baseMapper.updateById(account) == 1 )
//            return 1;
        LambdaUpdateWrapper<Account> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
        lambdaUpdateWrapper.eq(Account::getAutoId, account.getAutoId())
                .set(Account::getUsername, account.getUsername())
                .set(Account::getPassword, account.getPassword())

                .set(Account::getEmail, account.getEmail())
                //      关注空属性 Tel
                .set(Account::getTel, account.getTel());
        return   baseMapper.update(account, lambdaUpdateWrapper );
    }
}
